
import imPayCommon from './imPayCommon/imPayCommon';
const regexUtils = require('../../../../utils/RegexUtils.js');
import * as cacheUtils from '../../../../model/CacheUtils';
const constants = require('../../../../common/Constants.js');
const posUtil = require('../commonPos.js');
const UmfDialog = require('../../../../utils/UmfDialog.js');
import { $Toptips } from '../../../../components/CompRouter';
const umfToast = require('../../../../utils/UmfToast.js');
let cacheManager = null;
const PARAM_SIZE = 3;
let storeId = null;
Page(Object.assign({
  /**
   * 页面的初始数据
   */
  data: {
    pageInPos:true,
    cacheStorageData: {},
    qrCodeList: [{ index: 0, code: '', active: true }],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.posFrom=='detail'){
      wx.setNavigationBarTitle({
        title: '费率信息'
      })
    }
    this.setData({
      posFrom: options.posFrom,
      // cacheStorageData: options.feeRateInfoData != "undefined" ? JSON.parse(options.feeRateInfoData):""
    })
    // if (this.data.cacheStorageData){
    //   let qrCodeList=[]
    //   for (let i = 0; i < this.data.cacheStorageData.qrCodeListSubmit.length;i++){
    //     qrCodeList.push({ index: i, code: this.data.cacheStorageData.qrCodeListSubmit[i], active: true})
    //   }
    //   this.setData({
    //     qrCodeList: qrCodeList
    //   })
    // }
    //获取缓存
    storeId = options.storeId;
    
    cacheManager = new cacheUtils.cacheManager()
      .init("feeRateInfo" + storeId, constants.STORAGE_FUNC.FEERATE_INFO);
    let cacheStorage = cacheManager.getFunc();
    if (cacheStorage) {
      this.setData({
        cacheStorageData: cacheStorage,
        qrCodeList: cacheStorage.qrCodeList ? JSON.parse(cacheStorage.qrCodeList):this.data.qrCodeList
      })
    }
  },

  inputEvent: function (e) {
    let paramsKey = e.currentTarget.id;
    this._pageCacheStorage({ [paramsKey]: e.detail.value });
  },

  alertClick: function (e) {
    let itemKey = e.currentTarget.id;
    let msg = null;
    if (itemKey == "ts_jfeerate") {
      msg = "建议:标准商户签约费率0.48% ,20元封顶";
    } else if (itemKey == "ts_dfeerate") {
      msg = "建议:标准商户签约费率0.6%";
    }
    UmfDialog.show(msg);
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  _checkForm: function (data) {
    let formValues = data;
    //判断
    let resultErrorMsg = null;
    resultErrorMsg = regexUtils.regexTypeWithMsg(formValues["jfeerate"], constants.RegexTypes.JFEERATE);
    if (resultErrorMsg) {
      return resultErrorMsg;
    }
    resultErrorMsg = regexUtils.regexTypeWithMsg(formValues["debitCapamt"], constants.RegexTypes.DEBITCAPAMT);
    if (resultErrorMsg) {
      return resultErrorMsg;
    }

    resultErrorMsg = regexUtils.regexTypeWithMsg(formValues["dfeerate"], constants.RegexTypes.JFEERATE);
    if (resultErrorMsg) {
      return "请输入正确的贷记卡费率";
    }
    if (this.data.posFrom=='basis'){
      return this._imCheckForm(data.wxrate, data.alirate)
    }else 
    return ''
  },

  //提交按钮
  InfoSubmit: function (e) {
    let formData = e.detail.value;
    let errorMsg = this._checkForm(formData);  
    posUtil.pageSubmit($Toptips, errorMsg);
  },

  /**
  * 缓存统一处理
  * @param params
  * @param flag
  */
  _pageCacheStorage(params) {
    posUtil.cacheStorage({ "cacheManager": cacheManager, "params": params, "page": this, "pageName": "feeRateInfo", "minLength": PARAM_SIZE }, storeId);
  },
}, imPayCommon))